using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tool
{
    public partial class EventNameTextInput : Form
    {
        DuplicateEventNameDlg duplicateDlg;
        public bool m_renamingEvnt;
        
        public System.Windows.Forms.Form m_parent;

        public EventNameTextInput( System.Windows.Forms.Form pForm )
        {
            InitializeComponent();
            m_parent = pForm;
            duplicateDlg = new DuplicateEventNameDlg();
            m_renamingEvnt = false;
        }

        public void ButtonOK_Click(object sender, EventArgs e)
        {
            if (m_renamingEvnt)
            {
                if (((Editor)m_parent).GetEventTree().AddEvent(EventName.Text) ||
                    EventName.Text == ((Editor)m_parent).EventTree.SelectedNode.Text)
                {
                    ((Editor)m_parent).EventTree.SelectedNode.Text = EventName.Text;
                    m_renamingEvnt = false;
                    this.Close();
                }
                else
                {
                    duplicateDlg.ShowDialog();
                }

                return;
            }

            // Create a new node and set its name
            TreeNode RootNode = new TreeNode();
            RootNode.ContextMenuStrip = ((Editor)m_parent).EventsRightClickMenu;
            RootNode.Text = EventName.Text;
            RootNode.Tag = EventName.Text;

            if (EventName.Text == "")
            {
                return;
            }

            if (((Editor)m_parent).GetEventTree().AddEvent(RootNode.Text))
            {
                // Add the node to the tree
                ((Editor)m_parent).AddEventNode(RootNode);
                EventName.Clear();
                EventName.Focus();
                this.Close();      
            }
            else
            {
                duplicateDlg.ShowDialog();                
            }

            // Clear the text field so it's empty the next time an event is added
            EventName.Clear();
            EventName.Focus();
        }

        private void InputText_Load(object sender, EventArgs e)
        {
            EventName.Focus();
        }

        private void InputText_Close(object sender, EventArgs e)
        {
            m_renamingEvnt = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            EventName.Focus();
            EventName.Clear();
        }      
    }
}